﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebVideo.Mailers;
namespace WebVideo.Controllers
{
    public class MailerController : Controller
    {
        //
        // GET: /Mailer/

        public ActionResult Index()
        {
            return View();
        }
        #region "gửi mail kích hoạt tài khoản"
        public ActionResult Mailer(string User)
        {
            ViewBag.User = User;
            UserMailer.KichHoat(User).Send();

            //Send() extension method: using Mvc.Mailer
            // return View();
            return RedirectToAction("KichHoatTaiKhoan", "LoginUser");
        }
        #endregion
        #region "Gửi mail khi hết hạn lượt view video"
        public ActionResult Mailer1(int IdV)
        {
            ViewBag.IdV= IdV;
            UserMailer.ReportView(IdV).Send();
            //Send() extension method: using Mvc.Mailer
            // return View();
            return RedirectToAction("Index", "Index");
        }
        #endregion
        #region "Gửi mail  lượt view video còn 100"
        public ActionResult Mailer2(int IdV)
        {

            ViewBag.IdV = IdV;
            UserMailer.ReportView2(IdV).Send();
            //Send() extension method: using Mvc.Mailer
            // return View();
            return RedirectToAction("Index", "Index");
        }
        #endregion
        #region "Gửi mail khôi phục mật khẩu"
        public string KhoiPhuc(string Email)
        {

            ViewBag.Email = Email;
            UserMailer.KhoiPhucmk(Email).Send();
            return "Complete";
        }
        #endregion

        #region "Gia hạn lượt view"
        public string GuiMaXacNhan(int IdVideo, int LuotView)
        {
            try
            {
                UserMailer.GuiMaXacNhan(IdVideo, LuotView).Send();
                return "Complete";
            }
            catch
            {
                return "Error";
            }
        }

        public string GuiThongBaoGiaHanThanhCong(int IdVideo, int LuotView, string Ma)
        {
            try
            {
                //kiểm tra
                dbVideoDataContext db = new dbVideoDataContext();
                EntityVideo query = db.EntityVideos.Where(g => g.Id == IdVideo).First();
                string Maxn = IdVideo.ToString() + "&" + LuotView.ToString() + "&" + query.Video.ToString();
                if (Maxn == Ma)
                {
                    EntityReport_View w = new EntityReport_View();
                    w.UserName = query.EntityBusiness.UserName;
                    w.IdVideo = IdVideo;
                    w.ViewExtend = LuotView;
                    w.Date = DateTime.Now;
                    w.Status = 1;
                    db.EntityReport_Views.InsertOnSubmit(w);
                    // tăng lượt view
                    query.Views = query.Views + LuotView;
                    query.Status = 1;
                    query.Viewed_Date = null;
                    db.SubmitChanges();
                    UserMailer.GuiThongBaoGiaHanThanhCong(IdVideo, LuotView).Send();
                    return "Complete";
                }

                return "Error";
            }
            catch
            {
                return "Error";
            }
        }
        #endregion

        private IUserMailer _userMailer = new UserMailer();
        public IUserMailer UserMailer
        {
            get { return _userMailer; }
            set { _userMailer = value; }
        }
        public ActionResult KichHoat()
        {
            return View();
        }
        #region "Gửi mail doi diem thuong"
        public ActionResult Doidiem(string mail, int sotien, string name, string taikhoan, string nganhang)
        {
            ViewBag.mail = mail;
            UserMailer.Doidiemthuong(mail,sotien,name,taikhoan,nganhang).Send();
            return RedirectToAction("Index", "Admin/ReportPoint");
        }
        #endregion
    }
}
